package vn.com.vega.chacha.v2;

import java.util.ArrayList;
import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.Asynctask.LoadRecentlySongListen;
import vn.com.vega.chacha.v2.Asynctask.LoadRecentlySongListen.LoadRecentlyListener;
import vn.com.vega.music.clientserver.JsonSong;
import vn.com.vega.music.utils.Util;
import vn.com.vega.music.view.adapter.SongListAdapter;
import vn.com.vega.music.view.adapter.base.AutoLoadingAdapter;
import vn.com.vega.music.view.adapter.base.AutoLoadingListener;
import vn.com.vega.music.view.custom.BottomPlayerView;
import vn.com.vega.music.view.custom.BottomPlayerView.ShowHideBottomPlayerListener;
import vn.com.vega.music.view.custom.LoadingView;
import vn.com.vega.music.view.custom.TopTaskbarView;
import android.app.Activity;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

public class SongListenRecently extends ActivityBase implements
		AutoLoadingListener, OnClickListener {
	private ListView mListSong;
	private SongListAdapter mAdapter;
	private LoadingView mLoadingView;
	private LoadRecentlySongListen mLoadListSongRecenlty;
	private TopTaskbarView mTaskbarView;
	private BottomPlayerView mBottomPlayer;
	private View mPlayerViewReplace;
	private View mPlayAllSong;
	private View mSelectSongPlay;

	@Override
	public Activity getContext() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public List<Object> getMoreData() {
		// TODO Auto-generated method stub
		List<Object> result = new ArrayList<Object>();
		JsonSong jsonSong = JsonSong
				.loadSongListenRecently(mAdapter.getCount());
		if (jsonSong.isSuccess()) {
			result.addAll(jsonSong.songs);
		}
		return result;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mLoadListSongRecenlty != null
				&& mLoadListSongRecenlty.getStatus() == Status.RUNNING) {
			mLoadListSongRecenlty.cancel(true);
		}
	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.song_listen_recently);
		mListSong = (ListView) findViewById(R.id.lv_list_song);
		mLoadingView = (LoadingView) findViewById(R.id.loadingView_loading_list_song_listen_recently);
		mTaskbarView = (TopTaskbarView) findViewById(R.id.topTaskbarView_song_listen_recently);
		mTaskbarView.showBackButton();
		mTaskbarView.changeToTitleOnly("Nhạc nghe gần đây");
		mPlayerViewReplace = findViewById(R.id.view_replace_bottom_player);
		mBottomPlayer = (BottomPlayerView) findViewById(R.id.bottomPlayerView_song_listen_recently);
		mBottomPlayer.setShowHideListener(showHideBottomPlayerListener);

		mSelectSongPlay = findViewById(R.id.tv_select_song_play);
		mPlayAllSong = findViewById(R.id.tv_play_all_song);
		mSelectSongPlay.setOnClickListener(this);
		mPlayAllSong.setOnClickListener(this);

		mLoadListSongRecenlty = new LoadRecentlySongListen(this, listener);
		mLoadListSongRecenlty.execute();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mBottomPlayer.startUpdate();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mBottomPlayer.stopUpdate();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_select_song_play:
			if (mAdapter != null && mAdapter.getCount() > 0) {
//				Appinstant app = (Appinstant) getApplication();
//				app.tranportString.put(SelecSongPlay.KEY_LIST_SONG_TITLE,
//						"List song listen recently");
				mAdapter.selectSongPlay("Nhạc nghe gần đây");
			}
			break;
		case R.id.tv_play_all_song:
			if (mAdapter != null && mAdapter.getCount() > 0) {
				mAdapter.playAllSong("Song listen recently");
			}
			break;

		default:
			break;
		}
	}

	ShowHideBottomPlayerListener showHideBottomPlayerListener = new ShowHideBottomPlayerListener() {

		@Override
		public void onShowBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.VISIBLE);
		}

		@Override
		public void onHideBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.GONE);
		}
	};

	private LoadRecentlyListener listener = new LoadRecentlyListener() {

		@Override
		public void startLoading() {
			// TODO Auto-generated method stub
			mLoadingView.startLoading();
		}

		@Override
		public void loadSuccess(List<Object> songRecntly) {
			// TODO Auto-generated method stub
			mAdapter = new SongListAdapter(SongListenRecently.this,
					songRecntly, false, SongListenRecently.class.getName());
			mListSong.setAdapter(new AutoLoadingAdapter(mAdapter,
					SongListenRecently.this));
			mLoadingView.loadingFinish(true, null);
		}

		@Override
		public void loadFail(String message) {
			// TODO Auto-generated method stub
			if (Util.checkStringIsemptyOrNull(message)) {
				mLoadingView.loadingFinish(false, null);
			} else {
				mLoadingView.loadingFinish(false, message);
			}
		}
	};

}
